﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Croxweb.Leuphana.ViewModels;

namespace Croxweb.Leuphana.Pages
{
    public partial class PersonSearchPage : PhoneApplicationPage
    {
        private PersonSearchViewModel viewModel;

        public PersonSearchPage()
        {
            InitializeComponent();

            viewModel = new PersonSearchViewModel(App.MyStudyService, App.Config);
            DataContext = viewModel;
        }

        private void SearchBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                viewModel.Search(SearchBox.Text);
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PersonViewModel person = ((ListBox)sender).SelectedItem as PersonViewModel;
            if (null != person)
            {
                NavigationService.Navigate(new Uri("/Pages/PersonPage.xaml?id=" + person.Id, UriKind.Relative));
                ((ListBox)sender).SelectedIndex = -1;
            }
        }
    }
}